#include"PageCache.h"
PageCache PageCache::_instance;
Span* PageCache::NewSpan(size_t k)
{
	assert(k > 0&&k < NPAGES);
	if (!_span_list[k].IsEmpty())
	{
		return _span_list[k].PopFront();
	}
	for (size_t i = k + 1; i < NPAGES; i++)
	{
		if (!_span_list[i].IsEmpty())
		{
			Span* nspan = _span_list[i].PopFront();
			Span* kspan = new Span;
			kspan->page_id = nspan->page_id;
			kspan->n = k;
			nspan->n -= k;
			nspan->page_id += k;
			_span_list[nspan->n].PushFront(nspan);
			return kspan;
		}
	}
	Span* bigspan = new Span;
	void* ptr = SystemAlloc(NPAGES-1);
	bigspan->page_id = (PAGE_ID)ptr >> PAGE_SHIFT;
	bigspan->n = NPAGES-1;
	_span_list[bigspan->n].PushFront(bigspan);
	return NewSpan(k);
}